package Q1968

import java.util.*

/**
 * 构造元素不等于两相邻元素平均值的数组
 * @author 23737
 * @date 2022.4.28
 */
class Main {

}

fun main() {
    val nums = intArrayOf(1, 2, 3, 4, 5)
    for (i in Solution().rearrangeArray(nums)) {
        print(" $i ")
    }
}


class Solution {
    /**
     * 排序
     */
    fun rearrangeArray(nums: IntArray): IntArray {
        Arrays.sort(nums)
        var res: IntArray = IntArray(nums.size)
        val m = nums.size / 2
        for (i in 0 until nums.size) {
            when {
                i < m || i == m && nums.size % 2 == 1 -> res[2 * i] = nums[i]
                else -> res[2 * (i - m + 1 - nums.size % 2) - 1] = nums[i]
            }
        }
        return res
    }
}